package com.ossjk.qlh.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ossjk.config.mvc.MvcConfiguration;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonConstant;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.vo.RequestBean;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.qlh.crm.entity.Enterprise;
import com.ossjk.qlh.crm.service.IEnterpriseService;
import com.ossjk.qlh.crm.vo.EnterpriseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javafx.scene.input.DataFormat;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 企业
 *
 * @author parker
 * @since 2020-08-12
 */
@Api(tags = "企业")
@RestController
@RequestMapping("/crm/enterprise")
public class EnterpriseController extends BaseController {

    @Autowired
    private IEnterpriseService iEnterpriseService;

    @ApiOperation(value = "列表")
    @RequiresPermissions("00300000")
    @PostMapping("/list")
    public ResponseBean<Page<Enterprise>> list(@RequestBody RequestBean requestBean) {
        //是否过期逻辑还未出来

        //获取签约选择框
        String type = (String) requestBean.getParam().get("type");
        String ename = (String) requestBean.getParam().get("ename");

        //现在的时间 》 签约时间 = 过期
        String endtime = (String) requestBean.getParam().get("endtime");
        System.out.println("查询时间" + endtime);


        QueryWrapper<EnterpriseVo> queryWrapper = new QueryWrapper<EnterpriseVo>();
        if (!CommonUtil.isBlank(type)) {
            queryWrapper.eq("type", type);
        }
        if (!CommonUtil.isBlank(ename)) {
            queryWrapper.like("ename", ename);
        }

        if (!CommonUtil.isBlank(endtime)) {
            queryWrapper.lt("endtime", endtime);
        }

        //查询所有数组
        List<EnterpriseVo> enterpriseVos = iEnterpriseService.selectQY(queryWrapper);
        //封装数据

        for (EnterpriseVo enterpriseVo : enterpriseVos) {
            if (!CommonUtil.isBlank(enterpriseVo.getEndlch()) && !CommonUtil.isBlank(enterpriseVo.getEnddnr())) {//两餐
                enterpriseVo.setInterval("午餐/晚餐");//送餐时段

                enterpriseVo.setCuttime(new SimpleDateFormat("HH:mm").format(enterpriseVo.getEndlch()) + "/" + new SimpleDateFormat("HH:mm").format(enterpriseVo.getEnddnr()));//截至时间
            } else if (!CommonUtil.isBlank(enterpriseVo.getEndlch())) {//午餐
                enterpriseVo.setInterval("午餐");

                enterpriseVo.setCuttime(new SimpleDateFormat("HH:mm").format(enterpriseVo.getEndlch()) + "");
            } else {//晚餐
                enterpriseVo.setInterval("晚餐");
                enterpriseVo.setCuttime("" + new SimpleDateFormat("HH:mm").format(enterpriseVo.getEnddnr()));
            }

        }

        return responseSuccess(enterpriseVos);
    }

    @ApiOperation(value = "去修改")
    @RequiresPermissions("003000010000")
    @PostMapping("/toUpdate")
    public ResponseBean<Enterprise> toUpdate(@RequestBody RequestBean requestBean) {
        Enterprise enterprise = iEnterpriseService.getById((Serializable) requestBean.getParam().get("id"));

        if (!CommonUtil.isBlank(enterprise)) {
            return responseSuccess(enterprise);
        } else {
            return responseFail();
        }
    }

    @ApiOperation(value = "添加")
    @RequiresPermissions("003000000000")
    @ApiOperationSupport(ignoreParameters = {"id", "crer", "mder", "crtm", "mdtm"})
    @PostMapping("/save")
    public ResponseBean save(@RequestBody RequestBean<Enterprise> requestBean) {
        String code = (String) requestBean.getParam().get("code");
        List<Enterprise> list = iEnterpriseService.list();
        for (Enterprise enterprise : list) {
            if (CommonUtil.isEquals(enterprise.getCode(), code)) {
                return responseFail("企业码重复");
            }
        }

        if (iEnterpriseService.save(requestBean.getData())) {
            return responseSuccess("新增成功");
        } else {
            return responseFail("新增失败");
        }
    }

    @ApiOperation(value = "修改")
    @RequiresPermissions("003000010000")
    @ApiOperationSupport(ignoreParameters = {"crer", "mder", "crtm", "mdtm"})
    @PostMapping("/update")
    public ResponseBean update(@RequestBody RequestBean<Enterprise> requestBean) {

        String code = (String) requestBean.getParam().get("code");
        //排除本身以外，企业码不可重复
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.ne("code",code);
        List<Enterprise> list = iEnterpriseService.list(wrapper);
        for (Enterprise enterprises : list) {
            if (CommonUtil.isEquals(enterprises.getCode(), code)) {
                return responseFail("企业码重复");
            }
        }

        if (iEnterpriseService.updateById(requestBean.getData())) {
            return responseSuccess();
        } else {
            return responseFail();
        }
    }

    @ApiOperation(value = "删除")
    @RequiresPermissions("003000030000")
    @PostMapping("/remove")
    public ResponseBean delete(@RequestBody RequestBean requestBean) {
        List ids = (List) requestBean.getParam().get("ids");
        if (iEnterpriseService.removeByIds(ids)) {
            return responseSuccess();
        } else {
            return responseFail();
        }
    }


    @ApiOperation(value = "上传图片")
    @RequiresPermissions("003000000000")
    @PostMapping("/uploadPic")
    public ResponseBean uploadPic(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        //获取静态路径
//        String filePath = mvcConfiguration.getStaticsPath();
        String realpath = "upload";
        // 构建上传文件的存放路径
        String filePath = "/usr/local/";
        //设置文件夹名
        String imgFile = "crm";

        File picPathFile = new File(filePath + File.separator + imgFile + File.separator);
        if (!picPathFile.exists()) {
            picPathFile.mkdirs();
        }

        //设置文件名
        String picName = CommonUtil.formatDate(new Date(), CommonConstant.STRING_DATE_FORMAT6) + file.getOriginalFilename();

//        System.out.println(picPathFile + ",,,," + picName);

        //保存到本地
        file.transferTo(new File(picPathFile, picName));

        //响应返回文件地址+文件名
        return responseSuccess("上传成功", "http://127.0.0.1:8083/QLH-Vue-Api/" + realpath + "/" + imgFile + "/" + picName);
    }


}
